

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="ShanCe">
  <meta name="keywords" content="">
  
    <meta name="description" content="一、重写doRollover，实现文件的多进程锁(时间分割)1.Python多进程日志轮转多进程条件下 celery task中的日志写入可能会出现一系列问题 2.python 自带日志处理器基础日志模块 logging — Logging facility for Python — Python 3.12.1 documentation 基于基础模块的日志文件处理模块 logging.handl">
<meta property="og:type" content="article">
<meta property="og:title" content="python的logging实现多进程安全">
<meta property="og:url" content="https://sweetshance.github.io/2023/12/25/python%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86%E7%82%B9/python%E7%9A%84logging%E5%AE%9E%E7%8E%B0%E5%A4%9A%E8%BF%9B%E7%A8%8B%E5%AE%89%E5%85%A8/index.html">
<meta property="og:site_name" content="ShanCe的博客">
<meta property="og:description" content="一、重写doRollover，实现文件的多进程锁(时间分割)1.Python多进程日志轮转多进程条件下 celery task中的日志写入可能会出现一系列问题 2.python 自带日志处理器基础日志模块 logging — Logging facility for Python — Python 3.12.1 documentation 基于基础模块的日志文件处理模块 logging.handl">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-12-25T03:15:08.000Z">
<meta property="article:modified_time" content="2023-12-25T03:24:36.091Z">
<meta property="article:author" content="ShanCe">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>python的logging实现多进程安全 - ShanCe的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"sweetshance.github.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>ShanCe</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="python的logging实现多进程安全"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        ShanCe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-12-25 11:15" pubdate>
          2023年12月25日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          8.4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          71 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">python的logging实现多进程安全</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="一、重写doRollover，实现文件的多进程锁-时间分割"><a href="#一、重写doRollover，实现文件的多进程锁-时间分割" class="headerlink" title="一、重写doRollover，实现文件的多进程锁(时间分割)"></a>一、重写doRollover，实现文件的多进程锁(时间分割)</h2><h3 id="1-Python多进程日志轮转"><a href="#1-Python多进程日志轮转" class="headerlink" title="1.Python多进程日志轮转"></a>1.Python多进程日志轮转</h3><p>多进程条件下 celery task中的日志写入可能会出现一系列问题</p>
<h3 id="2-python-自带日志处理器"><a href="#2-python-自带日志处理器" class="headerlink" title="2.python 自带日志处理器"></a>2.python 自带日志处理器</h3><p>基础日志模块</p>
<p><a target="_blank" rel="noopener" href="https://docs.python.org/3/library/logging.html">logging — Logging facility for Python — Python 3.12.1 documentation</a></p>
<p>基于基础模块的日志文件处理模块</p>
<p><a target="_blank" rel="noopener" href="https://docs.python.org/3/library/logging.handlers.html">logging.handlers — 日志处理程序 — Python 3.12.1 文档</a></p>
<h3 id="TimedRotatingFileHandler"><a href="#TimedRotatingFileHandler" class="headerlink" title="TimedRotatingFileHandler"></a>TimedRotatingFileHandler</h3><p>基于 logging.handlers 的 支持在特定时间隔轮换磁盘日志文件  支持多线程下的日志文件写入</p>
<p>重要参数：</p>
<ol>
<li><p><em>when</em> 轮转间隔  </p>
</li>
<li><p><em>interval</em> 时间间隔的数量，默认为 1；例如，当 when=‘D’ 且 interval=7 时，表示每周轮转一次</p>
</li>
<li><p>backupCount：表示日志保存个数；当生成的日志文件数量超过该数目时，会自动删除旧的日志文件；默认值为 0</p>
</li>
<li><p>delay：是否延时打开文件，可选值为 True 和 False；当为 True 时，表示延时打开文件，即在第一次写入日志时才打开日志文件；当为 False 时，表示在初始化时即打开日志文件；默认值为 False；</p>
</li>
</ol>
<p>主要方法：</p>
<ol>
<li><em>computeRollover</em>  根据指定的时间计算出滚动时间</li>
<li><em>getFilesToDelete</em>  获取当前要删除日志文件列表</li>
<li><em>doRollover</em>  主要轮转方法</li>
</ol>
<h3 id="多进程下基于TimedRotatingFileHandler重写方法"><a href="#多进程下基于TimedRotatingFileHandler重写方法" class="headerlink" title="多进程下基于TimedRotatingFileHandler重写方法"></a>多进程下基于TimedRotatingFileHandler重写方法</h3><p>由于实际生产过程中 我们还有多进程的情景 在多进程下 TimedRotatingFileHandler 不能满足需求  会出现日志命名错误导致的日志写入问题</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> os<br><span class="hljs-keyword">import</span> platform<br><span class="hljs-keyword">import</span> time<br><span class="hljs-keyword">from</span> logging.handlers <span class="hljs-keyword">import</span> TimedRotatingFileHandler<br><br><span class="hljs-keyword">from</span> settings <span class="hljs-keyword">import</span> BASE_DIR<br><br><span class="hljs-keyword">if</span> platform.system() != <span class="hljs-string">&#x27;Windows&#x27;</span>:<br>    <span class="hljs-keyword">import</span> fcntl<br><br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">MultiCompatibleTimedRotatingFileHandler</span>(<span class="hljs-title class_ inherited__">TimedRotatingFileHandler</span>):<br>    <span class="hljs-comment"># 重写 doRollover 方法</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">doRollover</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">        do a rollover; in this case, a date/time stamp is appended to the filename</span><br><span class="hljs-string">        when the rollover happens.  However, you want the file to be named for the</span><br><span class="hljs-string">        start of the interval, not the current time.  If there is a backup count,</span><br><span class="hljs-string">        then we have to get a list of matching filenames, sort them and remove</span><br><span class="hljs-string">        the one with the oldest suffix.</span><br><span class="hljs-string">        &quot;&quot;&quot;</span><br>        <span class="hljs-keyword">if</span> self.stream:<br>            self.stream.close()<br>            self.stream = <span class="hljs-literal">None</span><br>        <span class="hljs-comment"># get the time that this sequence started at and make it a TimeTuple</span><br>        currentTime = <span class="hljs-built_in">int</span>(time.time())<br>        dstNow = time.localtime(currentTime)[-<span class="hljs-number">1</span>]<br>        t = self.rolloverAt - self.interval<br>        <span class="hljs-keyword">if</span> self.utc:<br>            timeTuple = time.gmtime(t)<br>        <span class="hljs-keyword">else</span>:<br>            timeTuple = time.localtime(t)<br>            dstThen = timeTuple[-<span class="hljs-number">1</span>]<br>            <span class="hljs-keyword">if</span> dstNow != dstThen:<br>                <span class="hljs-keyword">if</span> dstNow:<br>                    addend = <span class="hljs-number">3600</span><br>                <span class="hljs-keyword">else</span>:<br>                    addend = -<span class="hljs-number">3600</span><br>                timeTuple = time.localtime(t + addend)<br>        dfn = self.rotation_filename(self.baseFilename + <span class="hljs-string">&quot;.&quot;</span> +<br>                                     time.strftime(self.suffix, timeTuple))<br>        <span class="hljs-comment"># if os.path.exists(dfn):</span><br>        <span class="hljs-comment">#     os.remove(dfn)</span><br>        <span class="hljs-comment"># self.rotate(self.baseFilename, dfn)</span><br>        <span class="hljs-keyword">if</span> platform.system() == <span class="hljs-string">&#x27;Windows&#x27;</span>:  <span class="hljs-comment"># windows下文件锁方式待添加</span><br>            <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> os.path.exists(dfn) <span class="hljs-keyword">and</span> os.path.exists(self.baseFilename):<br>                self.rotate(self.baseFilename, dfn)<br>        <span class="hljs-keyword">else</span>:<br>            <span class="hljs-comment"># 兼容多进程并发 LOG_ROTATE</span><br>            <span class="hljs-comment"># 重写部位  加锁</span><br>            <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> os.path.exists(dfn):<br>                f = <span class="hljs-built_in">open</span>(self.baseFilename, <span class="hljs-string">&#x27;a&#x27;</span>)<br>                fcntl.lockf(f.fileno(), fcntl.LOCK_EX)<br>                <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> os.path.exists(dfn):<br>                    os.rename(self.baseFilename, dfn)<br>                <span class="hljs-comment"># 释放锁 释放老 log 句柄</span><br>                f.close()<br><br>        <span class="hljs-keyword">if</span> self.backupCount &gt; <span class="hljs-number">0</span>:<br>            <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> self.getFilesToDelete():<br>                os.remove(s)<br>        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> self.delay:<br>            self.stream = self._<span class="hljs-built_in">open</span>()<br>        newRolloverAt = self.computeRollover(currentTime)<br>        <span class="hljs-keyword">while</span> newRolloverAt &lt;= currentTime:<br>            newRolloverAt = newRolloverAt + self.interval<br>        <span class="hljs-comment">#If DST changes and midnight or weekly rollover, adjust for this.</span><br>        <span class="hljs-keyword">if</span> (self.when == <span class="hljs-string">&#x27;MIDNIGHT&#x27;</span> <span class="hljs-keyword">or</span> self.when.startswith(<span class="hljs-string">&#x27;W&#x27;</span>)) <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> self.utc:<br>            dstAtRollover = time.localtime(newRolloverAt)[-<span class="hljs-number">1</span>]<br>            <span class="hljs-keyword">if</span> dstNow != dstAtRollover:<br>                <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> dstNow:  <span class="hljs-comment"># DST kicks in before next rollover, so we need to deduct an hour</span><br>                    addend = -<span class="hljs-number">3600</span><br>                <span class="hljs-keyword">else</span>:           <span class="hljs-comment"># DST bows out before next rollover, so we need to add an hour</span><br>                    addend = <span class="hljs-number">3600</span><br>                newRolloverAt += addend<br>        self.rolloverAt = newRolloverAt<br></code></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://docs.python.org/3/library/fcntl.html#fcntl.lockf">fcntl — The fcntl and ioctl system calls — Python 3.12.1 documentation</a> –python中的 linux 文件方法</p>
<p>重写后的项目模块</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Logger</span>(<span class="hljs-title class_ inherited__">object</span>):<br>    <span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">    自定义logger类，用于处理不同地方引用输出到不同文件</span><br><span class="hljs-string">    &quot;&quot;&quot;</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, log_file</span>):<br>        self.log_file = log_file<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">get_logger</span>(<span class="hljs-params">self, name</span>):<br>        <span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">        初始化logger</span><br><span class="hljs-string">        :param name: 名字不同，获得的logger不同，否则是单例的logger，造成文件写入混乱</span><br><span class="hljs-string">        :return:</span><br><span class="hljs-string">        &quot;&quot;&quot;</span><br>        <span class="hljs-keyword">import</span> logging.handlers<br><br>        <span class="hljs-comment"># logging初始化工作</span><br>        logging.basicConfig()<br>        <span class="hljs-comment"># logger的初始化工作</span><br>        logger = logging.getLogger(name)<br>        logger.setLevel(logging.INFO)<br>        <span class="hljs-comment"># 添加TimedRotatingFileHandler</span><br><br>        <span class="hljs-comment"># logger.propagate = False</span><br><br>        log_path = os.path.join(BASE_DIR, self.log_file)<br><br>        <span class="hljs-comment"># interval 滚动周期，</span><br>        <span class="hljs-comment"># when=&quot;MIDNIGHT&quot;, interval=1 表示每天0点为更新点，每天生成一个文件</span><br>        <span class="hljs-comment"># backupCount  表示日志保存个数</span><br>        <span class="hljs-comment"># when=&#x27;MIDNIGHT&#x27;只要过了0点就会分割</span><br>        <span class="hljs-comment"># 原因是celery并没有帮我们实现切分日志的功能,以上TimedRotatingFileHandler的配置没有生效</span><br>        time_file_handler = MultiCompatibleTimedRotatingFileHandler(log_path, when=<span class="hljs-string">&#x27;MIDNIGHT&#x27;</span>, interval=<span class="hljs-number">1</span>,<br>                                                                    backupCount=<span class="hljs-number">7</span>, encoding=<span class="hljs-string">&#x27;utf8&#x27;</span>)<br>        <span class="hljs-comment"># 设置后缀名称，跟strftime的格式一样</span><br>        <span class="hljs-comment"># time_file_handler.suffix = &quot;%Y-%m-%d_%H-%M.log&quot;</span><br><br>        <span class="hljs-comment"># 需要注意的是suffix和extMatch一定要匹配的上，如果不匹配，过期日志不会被删除。</span><br>        formatter = logging.Formatter(<span class="hljs-string">&#x27;[%(process)s %(thread)d] %(asctime)s %(filename)s[line:%(lineno)d] &#x27;</span><br>                                      <span class="hljs-string">&#x27;%(levelname)s %(message)s&#x27;</span>)<br>        time_file_handler.setFormatter(formatter)<br>        logger.addHandler(time_file_handler)<br>        <span class="hljs-comment"># 关闭打开的文件</span><br>        time_file_handler.close()<br>        <span class="hljs-keyword">return</span> logger<br></code></pre></td></tr></table></figure>

<h2 id="二、使用第三方模块"><a href="#二、使用第三方模块" class="headerlink" title="二、使用第三方模块"></a>二、使用第三方模块</h2><ol>
<li><p>concurrent_log_handler<br>根据文件大小分割</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> logging<br><span class="hljs-keyword">from</span> concurrent_log_handler <span class="hljs-keyword">import</span> ConcurrentRotatingFileHandler<br><br>logger = logging.getLogger()<br>handler = ConcurrentRotatingFileHandler(filename=<span class="hljs-string">&#x27;test.log&#x27;</span>, mode=<span class="hljs-string">&#x27;a&#x27;</span>, maxBytes=<span class="hljs-number">1024</span>*<span class="hljs-number">1024</span>*<span class="hljs-number">10</span>, backupCount=<span class="hljs-number">5</span>, encoding=<span class="hljs-string">&#x27;utf-8&#x27;</span>, delay=<span class="hljs-literal">False</span>)<br>logger.addHandler(handler)<br><br>logger.info(<span class="hljs-string">&#x27;hello world&#x27;</span>)<br></code></pre></td></tr></table></figure></li>
<li><p>在django setting 中使用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><code class="hljs python">LOGGING = &#123;<br>    <span class="hljs-string">&#x27;version&#x27;</span>: <span class="hljs-number">1</span>,<br>    <span class="hljs-string">&#x27;disable_existing_loggers&#x27;</span>: <span class="hljs-literal">False</span>,<br>    <span class="hljs-string">&#x27;formatters&#x27;</span>: &#123; <span class="hljs-comment">#格式化器</span><br>        <span class="hljs-string">&#x27;standard&#x27;</span>: &#123;<br>            <span class="hljs-string">&#x27;format&#x27;</span>: <span class="hljs-string">&#x27;[%(levelname)s][%(asctime)s][%(filename)s][%(funcName)s][%(lineno)d] &gt; %(message)s&#x27;</span><br>        &#125;,<br>        <span class="hljs-string">&#x27;simple&#x27;</span>: &#123;<br>            <span class="hljs-string">&#x27;format&#x27;</span>: <span class="hljs-string">&#x27;[%(levelname)s]&gt; %(message)s&#x27;</span><br>        &#125;,<br>    &#125;,<br>    <span class="hljs-string">&#x27;filters&#x27;</span>: &#123;<br>        <span class="hljs-string">&#x27;require_debug_true&#x27;</span>: &#123;<br>            <span class="hljs-string">&#x27;()&#x27;</span>: <span class="hljs-string">&#x27;django.utils.log.RequireDebugTrue&#x27;</span>,<br>        &#125;,<br>    &#125;,<br>    <span class="hljs-string">&#x27;handlers&#x27;</span>: &#123;<br>        <span class="hljs-string">&#x27;error_handler&#x27;</span>: &#123;<br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;INFO&#x27;</span>,<br>            <span class="hljs-string">&#x27;class&#x27;</span>: <span class="hljs-string">&#x27;concurrent_log_handler.ConcurrentRotatingFileHandler&#x27;</span>,<br>            <span class="hljs-string">&#x27;filename&#x27;</span>: <span class="hljs-string">&#x27;%s/error.log&#x27;</span> % LOGGING_DIR,<br>            <span class="hljs-string">&#x27;maxBytes&#x27;</span>: <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">2</span>,<br>            <span class="hljs-string">&#x27;backupCount&#x27;</span>: <span class="hljs-number">2</span>,<br>            <span class="hljs-string">&#x27;formatter&#x27;</span>: <span class="hljs-string">&#x27;standard&#x27;</span>,<br>            <span class="hljs-string">&#x27;encoding&#x27;</span>: <span class="hljs-string">&#x27;utf-8&#x27;</span>,<br>        &#125;,<br>        <span class="hljs-string">&#x27;sql_handler&#x27;</span>: &#123;  <span class="hljs-comment"># sql 查询接口</span><br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;INFO&#x27;</span>,<br>            <span class="hljs-string">&#x27;class&#x27;</span>: <span class="hljs-string">&#x27;concurrent_log_handler.ConcurrentRotatingFileHandler&#x27;</span>,  <span class="hljs-comment"># 保存到文件，自动切</span><br>            <span class="hljs-string">&#x27;filename&#x27;</span>: <span class="hljs-string">&#x27;%s/sql.log&#x27;</span> % LOGGING_DIR,<br>            <span class="hljs-string">&#x27;maxBytes&#x27;</span>: <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">5</span>,  <span class="hljs-comment"># 日志大小 5M</span><br>            <span class="hljs-string">&#x27;backupCount&#x27;</span>: <span class="hljs-number">4</span>,<br>            <span class="hljs-string">&#x27;formatter&#x27;</span>: <span class="hljs-string">&#x27;standard&#x27;</span>,<br>            <span class="hljs-string">&#x27;encoding&#x27;</span>: <span class="hljs-string">&quot;utf-8&quot;</span><br>        &#125;,<br>        <span class="hljs-string">&#x27;request_handler&#x27;</span>: &#123;  <span class="hljs-comment"># 网络回去数据接口</span><br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;INFO&#x27;</span>,<br>            <span class="hljs-string">&#x27;class&#x27;</span>: <span class="hljs-string">&#x27;concurrent_log_handler.ConcurrentRotatingFileHandler&#x27;</span>,  <span class="hljs-comment"># 保存到文件，自动切</span><br>            <span class="hljs-string">&#x27;filename&#x27;</span>: <span class="hljs-string">&#x27;%s/request_data.log&#x27;</span> % LOGGING_DIR,<br>            <span class="hljs-string">&#x27;maxBytes&#x27;</span>: <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">5</span>,  <span class="hljs-comment"># 日志大小 5M</span><br>            <span class="hljs-string">&#x27;backupCount&#x27;</span>: <span class="hljs-number">4</span>,<br>            <span class="hljs-string">&#x27;formatter&#x27;</span>: <span class="hljs-string">&#x27;standard&#x27;</span>,<br>            <span class="hljs-string">&#x27;encoding&#x27;</span>: <span class="hljs-string">&quot;utf-8&quot;</span><br>        &#125;,<br>        <span class="hljs-string">&#x27;server_handler&#x27;</span>: &#123;  <span class="hljs-comment"># 请求别人接口日志</span><br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;INFO&#x27;</span>,<br>            <span class="hljs-string">&#x27;class&#x27;</span>: <span class="hljs-string">&#x27;concurrent_log_handler.ConcurrentRotatingFileHandler&#x27;</span>,  <span class="hljs-comment"># 保存到文件，自动切</span><br>            <span class="hljs-string">&#x27;filename&#x27;</span>: <span class="hljs-string">&#x27;%s/server_data.log&#x27;</span> % LOGGING_DIR,<br>            <span class="hljs-string">&#x27;maxBytes&#x27;</span>: <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">5</span>,  <span class="hljs-comment"># 日志大小 5M</span><br>            <span class="hljs-string">&#x27;backupCount&#x27;</span>: <span class="hljs-number">4</span>,<br>            <span class="hljs-string">&#x27;formatter&#x27;</span>: <span class="hljs-string">&#x27;standard&#x27;</span>,<br>            <span class="hljs-string">&#x27;encoding&#x27;</span>: <span class="hljs-string">&quot;utf-8&quot;</span><br>        &#125;,<br>        <span class="hljs-string">&#x27;integral_handler&#x27;</span>: &#123;  <span class="hljs-comment"># 请求别人接口日志</span><br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;INFO&#x27;</span>,<br>            <span class="hljs-string">&#x27;class&#x27;</span>: <span class="hljs-string">&#x27;concurrent_log_handler.ConcurrentRotatingFileHandler&#x27;</span>,  <span class="hljs-comment"># 保存到文件，自动切</span><br>            <span class="hljs-string">&#x27;filename&#x27;</span>: <span class="hljs-string">&#x27;%s/integral.log&#x27;</span> % LOGGING_DIR,<br>            <span class="hljs-string">&#x27;maxBytes&#x27;</span>: <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">5</span>,  <span class="hljs-comment"># 日志大小 5M</span><br>            <span class="hljs-string">&#x27;backupCount&#x27;</span>: <span class="hljs-number">4</span>,<br>            <span class="hljs-string">&#x27;formatter&#x27;</span>: <span class="hljs-string">&#x27;standard&#x27;</span>,<br>            <span class="hljs-string">&#x27;encoding&#x27;</span>: <span class="hljs-string">&quot;utf-8&quot;</span><br>        &#125;<br>    &#125;,<br>    <span class="hljs-string">&#x27;loggers&#x27;</span>: &#123;    <span class="hljs-comment">#日志分配到哪个handlers中</span><br>        <span class="hljs-string">&#x27;error&#x27;</span>: &#123;  <span class="hljs-comment"># 名为 &#x27;modify&#x27;的logger还单独处理</span><br>            <span class="hljs-string">&#x27;handlers&#x27;</span>: [<span class="hljs-string">&#x27;error_handler&#x27;</span>],<br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;DEBUG&#x27;</span>,<br>            <span class="hljs-string">&#x27;propagate&#x27;</span>: <span class="hljs-literal">False</span>,<br>        &#125;,<br>        <span class="hljs-string">&#x27;sql&#x27;</span>: &#123;<br>            <span class="hljs-string">&#x27;handlers&#x27;</span>: [<span class="hljs-string">&#x27;sql_handler&#x27;</span>],<br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;DEBUG&#x27;</span>,<br>            <span class="hljs-string">&#x27;propagate&#x27;</span>: <span class="hljs-literal">False</span>,<br>        &#125;,<br>        <span class="hljs-string">&#x27;request&#x27;</span>: &#123;<br>            <span class="hljs-string">&#x27;handlers&#x27;</span>: [<span class="hljs-string">&#x27;request_handler&#x27;</span>],<br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;DEBUG&#x27;</span>,<br>            <span class="hljs-string">&#x27;propagate&#x27;</span>: <span class="hljs-literal">False</span>,<br>        &#125;,<br>        <span class="hljs-string">&#x27;server&#x27;</span>: &#123;<br>            <span class="hljs-string">&#x27;handlers&#x27;</span>: [<span class="hljs-string">&#x27;server_handler&#x27;</span>],<br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;INFO&#x27;</span>,<br>            <span class="hljs-string">&#x27;propagate&#x27;</span>: <span class="hljs-literal">False</span>,<br>        &#125;,<br>        <span class="hljs-string">&#x27;integral&#x27;</span>: &#123;<br>            <span class="hljs-string">&#x27;handlers&#x27;</span>: [<span class="hljs-string">&#x27;integral_handler&#x27;</span>],<br>            <span class="hljs-string">&#x27;level&#x27;</span>: <span class="hljs-string">&#x27;INFO&#x27;</span>,<br>            <span class="hljs-string">&#x27;propagate&#x27;</span>: <span class="hljs-literal">False</span>,<br>        &#125;<br> &#125;<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>调用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> logging<br><br>ERROR_LOGIN = logging.getLogger(<span class="hljs-string">&quot;error&quot;</span>)  <span class="hljs-comment"># 一些错误日志</span><br>SERVER_LOGIN = logging.getLogger(<span class="hljs-string">&quot;server&quot;</span>)  <span class="hljs-comment"># 接口日志</span><br>REQUEST_LOGIN = logging.getLogger(<span class="hljs-string">&quot;request&quot;</span>)  <span class="hljs-comment"># 请求别人的接口日志</span><br>SQL_LOGIN = logging.getLogger(<span class="hljs-string">&quot;sql&quot;</span>)  <span class="hljs-comment"># sql 日志</span><br>INTEGRAL_LOGIN = logging.getLogger(<span class="hljs-string">&quot;integral&quot;</span>)  <span class="hljs-comment"># 积分相关日志</span><br><br>ERROR_LOGIN.error(<span class="hljs-string">&quot;错误&quot;</span>)<br></code></pre></td></tr></table></figure></li>
</ol>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>python的logging实现多进程安全</div>
      <div>https://sweetshance.github.io/2023/12/25/python相关知识点/python的logging实现多进程安全/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>ShanCe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年12月25日</div>
        </div>
      
      
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/12/26/%E7%AE%97%E6%B3%95/%E4%BA%8C%E5%8F%89%E6%A0%91/3-%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96/" title="3.二叉树的序列化和反序列化">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">3.二叉树的序列化和反序列化</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/12/21/%E7%AE%97%E6%B3%95/%E4%BA%8C%E5%8F%89%E6%A0%91/2-%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%81%8D%E5%8E%86/" title="2.二叉树的遍历">
                        <span class="hidden-mobile">2.二叉树的遍历</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="http://82.157.172.231/" target="_blank" rel="nofollow noopener"><span>无聊</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>ShanCe</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
